package com.ruoyi.framework.mq.RabbitMQ;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/mq")
public class SendMq {
    private static final Logger log=LoggerFactory.getLogger(SendMq.class);
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RabbitMQMsgUtil rabbitMQMsgUtil;

    class User{
        private String name;
        private Integer age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    /**
     * direct简单模式没有交换机
     */
    @RequestMapping("/hello")
    public void send() {
        String context = "hello " + new Date();
        log.info("发送消息 : " + context);
        MessagePojo messagePojo = new MessagePojo(context);
        rabbitMQMsgUtil.sendMessage("",RabbitMqConstants.DIRECT_ROUTINGKEY, messagePojo);
    }

    /**
     * direct简单模式有交换机
     */
    @RequestMapping("/directSend")
    public void directSend() {
        String context = "directSend " + new Date().toString();
        log.info("发送消息 : " + context);
        MessagePojo messagePojo = new MessagePojo(context);
        rabbitMQMsgUtil.sendMessage(RabbitMqConstants.DIRECT_EXCHANGE,RabbitMqConstants.DIRECT_ROUTINGKEY,messagePojo);
    }


    /**
     * work模式：一个生产者，多个消费者，每个消费者获取到的消息唯一
     */
    @RequestMapping("/workSend")
    public void workSend() {
        User user = null;
        for (int i = 0; i < 5; i++) {
            user = new User();
            user.setName("wxf");
            user.setAge(i);
            log.info("发送消息 : " + user);
            String s = user.toString();
            MessagePojo messagePojo = new MessagePojo(s);
            rabbitMQMsgUtil.sendMessage(RabbitMqConstants.DIRECT_EXCHANGE,RabbitMqConstants.DIRECT_ROUTINGKEY, messagePojo);
        }
    }

    /**
     * Fanout 广播 模式 :对应Fanout交换机的队列都会收到消息
     */
    @RequestMapping("/fanoutSend")
    public void fanoutSend() {
        User user = null;
        for (int i = 0; i < 5; i++) {
            user = new User();
            user.setName("wxf");
            user.setAge(18);
            log.info("发送消息 : " + user);
            String s = user.toString();
            MessagePojo messagePojo = new MessagePojo(s);
            rabbitMQMsgUtil.sendMessage(RabbitMqConstants.FANOUT_EXCHANGE,"",messagePojo);
        }
    }

    /**
     *  topic 模式 : 主题模式 匹配模式的路由
     */
    @RequestMapping("/topicSend")
    public void topicSend() {
        User user = null;
        String routingKeyA = "topic.wxf.w";
        for (int i = 0; i < 5; i++) {
            user = new User();
            user.setName("wxf");
            user.setAge(i);
            log.info("发送消息 : " + user);
            String s = user.toString();
            MessagePojo messagePojo = new MessagePojo(s);
            rabbitMQMsgUtil.sendMessage(RabbitMqConstants.TOPIC_EXCHANGE,routingKeyA,messagePojo);
        }
    }

    /**
     *  延迟队列ttl
     */
    @RequestMapping("/delayeSend")
    public void delayeSend() {
        User user = new User();
        user.setName("wxf");
        user.setAge(21);
        String msg = user.toString();
        MessagePojo messagePojo = new MessagePojo(msg);
        log.info("延迟队列ttl发送消息 : " + msg);
        rabbitMQMsgUtil.sendMessage(RabbitMqConfig.ExchangeName.DELAY, RabbitMqConfig.RoutingKey.DELAY_KEY,messagePojo );
    }


    /**
     *  延迟队列插件
     */
    @RequestMapping("/xdelayeSend")
    public void xdelayeSend() {
        User user = new User();
        user.setName("wxf");
        user.setAge(21);
        String msg = user.toString();
        log.info("延迟队列插件发送消息 : " + msg);
        MessagePojo messagePojo = new MessagePojo(msg);
        rabbitMQMsgUtil.sendMessage(RabbitMqConstants.DELAYED_EXCHANGE,RabbitMqConfig.RoutingKey.XDELAY_KEY,messagePojo,5000);
    }

}
